<template>
  <div class="user">
    <div class="search-user">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        ref="formInline"
      >
        <el-form-item label="账户" prop="name">
          <el-input
            v-model="formInline.name"
            placeholder="请输入用户账户"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="formInline.email"
            placeholder="请输入账户邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="formInline.role">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="访客" value="visitor"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formInline.status">
            <el-option label="锁定" value="unActive"></el-option>
            <el-option label="可用" value="active"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="operate">
          <el-button type="primary" @click="onSubmit()">查询</el-button>
          <el-button @click="resetForm('formInline')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-user">
      <!-- 头部 -->
      <div class="table-header">
        <el-button>刷新</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="name" label="账户" width="200">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="200">
        </el-table-column>
        <el-table-column prop="role" label="角色" width="200">
          <template slot-scope="scope">
            {{ role(scope.row.role) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="200">
          <template slot-scope="scope">
            <div class="state-box">
              <span
                :class="scope.row.status === 'active' ? 'active' : ''"
              ></span>
              {{ status(scope.row.status) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_at" label="注册日期" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operate">
              <el-button
                type="text"
                size="small"
                @click="editUser('status', 'unActive', scope.row.id)"
                v-if="scope.row.status === 'active'"
                >禁用</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="editUser('status', 'active', scope.row.id)"
                v-if="scope.row.status !== 'active'"
                >启用</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="editUser('role', 'admin', scope.row.id)"
                v-if="scope.row.role !== 'admin'"
                >授权</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="editUser('role', 'visitor', scope.row.id)"
                v-if="scope.row.role === 'admin'"
                >解除授权</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="currentChange"
        :page-size="pageSize"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      formInline: {
        name: "",
        email: "",
        role: "",
        status: "",
      },
      tableData: [],
      pageIndex: 1,
      pageSize: 8,
      total: 0,
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    onSubmit() {
      this.getUserList();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    editUser(key, value,id) {
      let obj = {};
      obj[key] = value;
      this.$api
        .editUser({
          id,
          ...obj,
        })
        .then((res) => {
          if (res.success) {
            this.$message({
              message: "操作成功",
              type: "success",
            });
          }
          // 更新页面
          this.getUserList();
        });
    },
    // 获取用户列表
    getUserList() {
      this.$api
        .getUserList({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          ...this.formInline,
        })
        .then((res) => {
          // 处理时间的格式
          res.data.data.forEach((item) => {
            item.create_at = item.create_at.split("T")[0];
          });
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },
    // 分页页码改变
    currentChange(index) {
      this.pageIndex = index;
      this.getUserList();
    },
    role(roleName) {
      if (roleName === "admin") {
        return "管理员";
      } else if (roleName === "visitor") {
        return "访客";
      }
    },
    status(statusName) {
      if (statusName === "active") {
        return "可用";
      } else {
        return "不可用";
      }
    },
  },
  components: {},
};
</script>
<style lang="scss" scoped>
.user {
  width: 100%;
  height: 100%;
  background-color: #eff2f5;
  display: flex;
  flex-direction: column;
  align-items: center;
  .search-user {
    width: 95%;
    height: 150px;
    background-color: #fff;
    margin-top: 20px;
    .el-form {
      display: flex;
      height: 150px;
      justify-content: space-around;
      align-items: center;
      .operate {
        width: 150px;
        height: 80%;
        display: flex;
        justify-content: end;
        align-items: flex-end;
      }
    }
  }
  .table-user {
    width: 95%;
    background-color: #fff;
    margin-top: 20px;
    /*滚动条样式*/
    ::-webkit-scrollbar {
      /* width: 4px; */
      height: 8px;
    }
    ::-webkit-scrollbar-thumb {
      border-radius: 10px;
      background: #dadada;
    }
    ::-webkit-scrollbar-track {
      border-radius: 0;
    }
    .table-header {
      height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .el-button {
        height: 40px;
      }
    }
    .cell {
      .state-box {
        display: flex;
        align-items: center;
        span {
          width: 10px;
          height: 10px;
          background-color: #faad14;
          margin-right: 5px;
          border-radius: 50%;
        }
        span.active {
          background-color: #5bc726;
        }
      }
    }
    .el-pagination {
      display: flex;
      justify-content: end;
    }
  }
}
</style>